<template>
  <el-dialog :title="isAdd?'新增部门':'编辑部门'" :visible="showDialog" @close="close" >
    <!-- 匿名插槽 -->
    <el-form label-width="120px" :model="form" :rules="rules" ref="deptForm">
      <el-form-item label="部门名称" prop="name">
        <el-input style="width:80%" placeholder="1-50个字符" v-model="form.name"/>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input style="width:80%" placeholder="1-50个字符" v-model="form.code"/>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select style="width:80%" placeholder="请选择" v-model="form.manager" @focus="getSelect">
          <el-option
            v-for="(item,index) in people"
            :key="item.index"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          style="width:80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
          v-model="form.introduce"
        />
      </el-form-item>
    </el-form>

    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addDepts">确定</el-button>
        <el-button size="small" @click="$emit('addDepts')">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import {
  getDepartments,
  addDepartments,
  editDepartments
} from "@/api/departments";
import { getEmployees } from "@/api/employees";
export default {
  methods: {
    //   添加修改部门
    addDepts() {
      if (this.isAdd) {
        //   添加部门
        this.$refs.deptForm.validate(async valid => {
          if (valid) {
            try {
              await addDepartments(this.form);
              this.$message.success("添加成功");
              this.$emit("addDepts");
            } catch (err) {}
          } else {
            return false;
          }
        });
      } else {
        //   修改部门
        this.$refs.deptForm.validate(async valid => {
          if (valid) {
            try {
              this.form.pid = "";
              await editDepartments(this.form);
              this.$message.success("修改成功");
              this.$emit("addDepts");
            } catch (err) {}
          } else {
            return false;
          }
        });
      }
    },
    close() {
      this.$refs.deptForm.resetFields();
    },
    async getSelect() {
      const data = await getEmployees();
      this.people = data;
      console.log(data);
    }
  },
  async created() {
    if (!this.isAdd) {
      const data = await getDepartments(this.id);
      console.log(data);
      this.form.code = data.code;
      this.form.introduce = data.introduce;
      this.form.manager = data.manager;
      this.form.name = data.name;
    }
  },
  props: {
    id: {
      type: [Number, String]
    },
    showDialog: {
      type: Boolean
    },
    isAdd: {
      type: Boolean
    }
  },
  data() {
    const checkNameRepeat = async (rule, value, callback) => {
      const { depts } = await getDepartments();
      depts
        .filter(item => {
          return item.pid === this.id;
        })
        .some(item => {
          return item.name === value
            ? callback(new Error("部门名称重复"))
            : callback();
        });
    };
    const checkCodeRepeat = async (rule, value, callback) => {
      // 先要获取最新的组织架构数据
      const { depts } = await getDepartments();
      depts.some(item => {
        return item.code === value && value
          ? callback(new Error("部门编码重复"))
          : callback();
      });
    };
    return {
      people: [],
      form: {
        code: "",
        introduce: "",
        manager: "",
        name: "",
        pid: this.id ? this.id : ""
      },
      rules: {
        code: [
          { required: true, message: "请输入活动部门编码", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符",
            trigger: "blur"
          },
          { validator: checkCodeRepeat, trigger: "blur" }
        ],
        introduce: [
          { required: true, message: "请输入活动部门介绍", trigger: "blur" },
          {
            min: 1,
            max: 300,
            message: "长度在 1 到 300 个字符",
            trigger: "blur"
          }
        ],
        manager: [
          { required: true, message: "请输入活动部门负责人", trigger: "blur" }
        ],
        name: [
          { required: true, message: "请输入活动部门名称", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符",
            trigger: "blur"
          },
          { validator: checkNameRepeat, trigger: "blur" }
        ]
      }
    };
  }
};
</script>
